package zhh.tangbao.commodity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import zhh.tangbao.commodity.constant.SellStatus;
import zhh.tangbao.commodity.dto.CommodityEntityQueryDto;
import zhh.tangbao.commodity.entity.CommodityEntity;
import zhh.tangbao.commodity.serivce.CommodityEntityService;
import zhh.tangbao.system.annotation.Authority;
import zhh.tangbao.system.annotation.Log;
import zhh.tangbao.system.constant.AuthorityModel;
import zhh.tangbao.system.result.Result;

import java.util.List;

/**
 * 2023/2/28
 * @author 粥灰灰
 * @version 1.0
 * 描述
 */

@RestController
@RequestMapping("/api/commodity")
public class CommodityEntityController {


    @Autowired
    private CommodityEntityService commodityEntityService;



    @GetMapping("/page")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "admin")
    public Result page(CommodityEntityQueryDto dto) {
        Page<CommodityEntity> page = commodityEntityService.page(dto);
        return Result.success().put(page);
    }

    @PostMapping("/add")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "commodity")
    @Log(function = "添加实体信息", moduleName = "商品实体信息管理")
    public Result add(@RequestBody CommodityEntity commodityEntity, Long imgFileId) {
        int add = commodityEntityService.add(commodityEntity, imgFileId);
        return Result.successOrFail(add);
    }

    @DeleteMapping("/delete/{id}")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "commodity")
    @Log(function = "删除实体信息", moduleName = "商品实体信息管理")
    public Result delete(@PathVariable("id") String id) {
        return Result.successOrFail(commodityEntityService.delete(id));
    }

    @PutMapping("/update")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "commodity")
    @Log(function = "更新实体信息", moduleName = "商品实体信息管理")
    public Result update(@RequestBody CommodityEntity commodityEntity) {
        return Result.successOrFail(commodityEntityService.update(commodityEntity));
    }

    @GetMapping("/get/{id}")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "commodity")
    public Result get(@PathVariable("id") String id) {
        return Result.success().put(commodityEntityService.get(id));
    }

    /**
     * 下架会连带把售货机中正在售卖的商品记录删除
     */
    @GetMapping("/takeOff/{id}")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "commodity")
    @Log(function = "商品下架", moduleName = "商品实体信息管理")
    public Result takeOff(@PathVariable("id") String id) {
        return Result.successOrFail(commodityEntityService.takeOffTheSell(id));
    }

    @GetMapping("/takeOn/{id}")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "commodity")
    @Log(function = "商品上架", moduleName = "商品实体信息管理")
    public Result takeOn(@PathVariable("id") String id) {
        return Result.successOrFail(commodityEntityService.takeOnTheSell(id));
    }

    /**
     * 检测该商品是否正在售货机中贩卖
     * @return 正在使用该商品的售货机ID
     */
    @GetMapping("/isUsing/{id}")
    @Authority(login = false)
    public Result isUsing(@PathVariable("id") String id) {
        return Result.success().put(commodityEntityService.isUsing(id));
    }

    @PostMapping("/batchTakeOn")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "commodity")
    @Log(function = "商品批量上架", moduleName = "商品实体信息管理")
    public Result batchTakeOn(@RequestBody List<String> ids) {
        return Result.successOrFail(commodityEntityService.batchChangeSellStatus(ids, SellStatus.TAKE_ON));
    }

    @PostMapping("/batchTakeOff")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "commodity")
    @Log(function = "商品批量下架", moduleName = "商品实体信息管理")
    public Result batchTakeOff(@RequestBody List<String> ids) {
        return Result.successOrFail(commodityEntityService.batchChangeSellStatus(ids, SellStatus.TAKE_OFF));
    }

    @PostMapping("/batchDelete")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "commodity")
    @Log(function = "商品批量删除", moduleName = "商品实体信息管理")
    public Result batchDelete(@RequestBody List<String> ids) {
        return Result.successOrFail(commodityEntityService.batchDelete(ids));
    }
}
